package com.ws.haungjia.controller;

import com.ws.haungjia.Bean.User;
import com.ws.haungjia.Service.GerenService;
import com.ws.haungjia.utils.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/geren")
public class Gerenzhongxin {
    @Autowired
    private GerenService geren;

    @RequestMapping("cha")
    public String chakna(int id, Model model ) {
        User cha = geren.cha(id);
        Model user = model.addAttribute("list", cha);
        return "WEB-INF/qianduan/gerzhongxin";
    }
    @RequestMapping("huixian")
    public String huixian(int id, Model model) {
        User huixian = geren.huixian(id);
        Model user = model.addAttribute("user", huixian);
        return "WEB-INF/qianduan/gerzhongxinxiugai";
    }

    @RequestMapping("xiugai")
    public String xiugai(String name, String mima, int shouji, int id, MultipartFile touxaing) throws IOException {
        User user = new User();
        user.setName(name);
        user.setShouji(shouji);
        user.setUserID(id);
        if (touxaing.getSize() > 0) {
            String replace = UUID.randomUUID().toString().replace("-", "");
            String fileName = replace + "." + touxaing.getOriginalFilename().split("\\.")[1];
            touxaing.transferTo(new File(Res.fileDir + fileName));
            user.setTouxiang(fileName);
        }else{
            user.setTouxiang("");
        }
        String s = DigestUtils.md5DigestAsHex(mima.getBytes());
        user.setPass(s);
        int add = geren.xiugai(user);
        if (add > 0) {
            return "redirect:/geren/cha?id="+id;
        }else {
            return "WEB-INF/qianduan/gerzhongxin";
        }
    }
}
